using System;
using System.Collections.ObjectModel;
using JMClient.Models.Common;
using Material.Icons;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation.Regions;
using System.Linq;
using DryIoc;
using JMClient.Views;
using Prism.Container.DryIoc;
using Prism.Ioc;

namespace JMClient.ViewModels.ControlsLib;

public class NavSliderViewModel : BindableBase
{
    private readonly IRegionManager _regionManager;
    private readonly IContainerRegistry _containerRegistry;

    public ObservableCollection<NavigationItem> NavigationItems { get; }
    public DelegateCommand<NavigationItem> NavigationCommand { get; set; }

    private NavigationItem _selectedNavigationItem;
    public NavigationItem SelectedNavigationItem
    {
        get => _selectedNavigationItem;
        set
        {
            if (SetProperty(ref _selectedNavigationItem, value) && value != null)
            {
                try
                {
                    var region = _regionManager.Regions["NavContentRegion"];
                    var viewType = Type.GetType($"JMClient.Views.{value.Id}");
                    var view = region.GetView(value.Id);
                    if (view is null && viewType is not null)
                        view = region.Views.FirstOrDefault(v => v?.GetType() == viewType);

                    if (view is not null)
                    {
                        //region.Activate(view);
                        _regionManager.RequestNavigate("NavContentRegion", value.Id);
                    }
                    else
                    {
                        _regionManager.RequestNavigate("NavContentRegion", value.Id);
                    }

 
                    // 选中变化才导航，避免构造函数里“提前导航”把 Region 清空
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
        
               
            }
        }
    }

    public NavSliderViewModel(IRegionManager regionManager, IContainerRegistry containerRegistry)
    {
        _regionManager = regionManager;
        _containerRegistry = containerRegistry;

        NavigationItems = new ObservableCollection<NavigationItem>
        {
            new() { Id = "HomeView", Title = "首页", Icon = MaterialIconKind.Home },
            new() { Id = "ComponentsList", Title = "组件", Icon = MaterialIconKind.ListBox },
            new() { Id = "DataCharts", Title = "数据", Icon = MaterialIconKind.ChartBellCurve },
            new() { Id = "DemoNotes", Title = "DemoBoard", Icon = MaterialIconKind.LabelVariant },
        };

        NavigationCommand = new DelegateCommand<NavigationItem>(item =>
        {
            if (item != null && !Equals(SelectedNavigationItem, item))
                SelectedNavigationItem = item; // 走同一条逻辑
        });

        // 不在构造函数里设置默认选中；等 View Ready 后再设
    }

    public void ActivateDefault()
    {
        if (SelectedNavigationItem == null && NavigationItems.Count > 0)
            SelectedNavigationItem = NavigationItems[0];
    }
}